package com.simen.hellobytes.zips;

import java.util.zip.Deflater;

/**
 * deflater算法压缩类
 * author: SimenHi
 * create time: 2020-08-28 10:39
 * description:
 */
public class DeflaterHelper {

    private byte[] input;
    private int compressBuffLength = 1024;
    private boolean wholeCompress;
    private byte[] data;
    private int compressedDataLength;
    private int flushMode;

    public DeflaterHelper(byte[] input) {
        this.input = input;
    }

    public DeflaterHelper compress() {
        if(wholeCompress) {
            compressBuffLength = input.length;

            byte[] outBuff = new byte[compressBuffLength];

            Deflater compresser = new Deflater();
            compresser.setInput(input);
            compresser.finish();
            int compressedDataLength = compresser.deflate(outBuff,0,outBuff.length,flushMode);
            compresser.end();

            this.data = outBuff;
            this.compressedDataLength = compressedDataLength;
        }else{

        }
        return this;
    }

    public boolean isWholeCompress() {
        return wholeCompress;
    }

    public DeflaterHelper setWholeCompress(boolean wholeCompress) {
        this.wholeCompress = wholeCompress;
        return this;
    }

    public DeflaterHelper setCompressBuffLength(int compressBuffLength) {
        this.compressBuffLength = compressBuffLength;
        return this;
    }

    public int getCompressBuffLength() {
        return compressBuffLength;
    }

    public byte[] getInput() {
        return input;
    }

    public DeflaterHelper setInput(byte[] input) {
        this.input = input;
        return this;
    }

    public byte[] getData() {
        return data;
    }

    public DeflaterHelper setData(byte[] data) {
        this.data = data;
        return this;
    }

    public int getCompressedDataLength() {
        return compressedDataLength;
    }

    public DeflaterHelper setCompressedDataLength(int compressedDataLength) {
        this.compressedDataLength = compressedDataLength;
        return this;
    }

    public int getFlushMode() {
        return flushMode;
    }

    public DeflaterHelper setFlushMode(int flushMode) {
        this.flushMode = flushMode;
        return this;
    }
}
